package com.app.service.impl;

import com.app.mapper.ConfigMapper;
import com.app.mapper.SkillTrainingMapper;
import com.app.mapper.TrainEnrollMapper;
import com.app.mapper.TrainTypeConfigSonMapper;
import com.app.model.*;
import com.app.service.ConfigService;
import com.app.service.TrainEnrollService;
import com.app.vo.TrainVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class TrainEnrollServiceImpl implements TrainEnrollService {

    @Autowired
    TrainEnrollMapper trainEnrollMapper;
    @Autowired
    ConfigMapper configMapper;
    @Autowired
    TrainTypeConfigSonMapper trainTypeConfigSonMapper;
    @Autowired
    SkillTrainingMapper skillTrainingMapper;
    @Autowired
    ConfigService configService;

    @Override
    public void insert(TrainEnrollModel model) {
        trainEnrollMapper.insert(model);
    }

    @Override
    public void update(TrainEnrollModel model) {
        trainEnrollMapper.updateById(model);
    }

    @Override
    public void delete(int id) {
        trainEnrollMapper.deleteById(id);
    }

    @Override
    public int selectByPhone(TrainEnrollModel model) {
        QueryWrapper<TrainEnrollModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select().eq("mobile", model.getMobile());
        int count = trainEnrollMapper.selectCount(queryWrapper);
        return count;
    }

    @Override
    public List<TrainEnrollModel> getSkillTrainingList(TrainEnrollModel model) {
        QueryWrapper<TrainEnrollModel> queryWrapper = new QueryWrapper<>();
        if (model.getCid() > 0) {
            queryWrapper.select().eq("cid", model.getCid());
        }
        if (model.getSid() > 0) {
            queryWrapper.select().eq("sid", model.getSid());
        }
        if (model.getId() > 0) {
            queryWrapper.select().eq("id", model.getId());
        }
        List<TrainEnrollModel> userList = trainEnrollMapper.selectList(queryWrapper);
        if (userList.isEmpty()) {
            return null;
        } else {
            return userList;
        }
    }

    @Override
    public TrainEnrollModel selectByCid(TrainEnrollModel model) {
        QueryWrapper<TrainEnrollModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select().eq("cid", model.getCid());
        TrainEnrollModel trainEnrollModel = trainEnrollMapper.selectOne(queryWrapper);
        return trainEnrollModel;
    }


    @Override
    public TrainEnrollModel selectById(Integer id) {
        return this.trainEnrollMapper.selectById(id);
    }

    @Override
    public Page<TrainEnrollModel> getModelList(TrainEnrollModel model) {

        QueryWrapper<TrainEnrollModel> queryWrapper = new QueryWrapper<>();
        if (model != null) {
            if (model.getSid() != null) {
                queryWrapper.eq("sid", model.getSid());
            }
        }
        queryWrapper.orderByDesc("crt_time");
        Page<TrainEnrollModel> page = new Page<>();
        page.setCurrent(model.getPage());
        page.setSize(model.getLimit());
        Page<TrainEnrollModel> jobFairSceneModelPage = trainEnrollMapper.selectPage(page, queryWrapper);
        if (jobFairSceneModelPage == null) {
            return null;
        } else {
            return jobFairSceneModelPage;
        }
    }

    @Override
    public Integer selectCount() {
        return trainEnrollMapper.selectCount(null);
    }

    @Override
    public TrainVo registrationList(Integer page1, Integer limit, Integer streetId) {
        page1 = (page1 - 1) * limit;
        List<TrainEnrollModel> trainEnrollModels = trainEnrollMapper.registrationList(page1, limit, streetId);
        QueryWrapper<ConfigModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("con_type", 4);
        QueryWrapper<ConfigModel> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("con_type", 2);
        List<ConfigModel> configModels = configMapper.selectList(queryWrapper);
        List<ConfigModel> configModels1 = configMapper.selectList(queryWrapper1);
        for (int i = 0; i < trainEnrollModels.size(); i++) {
            for (int i1 = 0; i1 < configModels.size(); i1++) {
                if (trainEnrollModels.get(i).getStreetId() != null && trainEnrollModels.get(i).getStreetId().equals(configModels.get(i1).getId())) {
                    trainEnrollModels.get(i).setStreetText(configModels.get(i1).getName());
                }
            }
            for (int i1 = 0; i1 < configModels1.size(); i1++) {
                if (trainEnrollModels.get(i).getStreetId() != null && trainEnrollModels.get(i).getStreetId().equals(configModels1.get(i1).getId())) {
                    trainEnrollModels.get(i).setStreetText(configModels1.get(i1).getName());
                }
            }
        }
        Integer count = trainEnrollMapper.registrationCount(page1, limit, streetId);
        TrainVo vo = new TrainVo();
        vo.setList(trainEnrollModels);
        vo.setCount(count);
        return vo;
    }

    @Override
    public Page<TrainEnrollModel> myApplication(Integer page, Integer limit, Integer status, CustomerModel user) {
        QueryWrapper<TrainEnrollModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select();
        if (status != null && status != 0) {
            queryWrapper.eq("status", status);
        }
        queryWrapper.eq("cid", user.getId());
        queryWrapper.orderByDesc("crt_time");
        Page<TrainEnrollModel> page1 = new Page<>();
        page1.setCurrent(page);
        page1.setSize(limit);
        Page<TrainEnrollModel> jobFairSceneModelPage = trainEnrollMapper.selectPage(page1, queryWrapper);

        if (jobFairSceneModelPage == null) {
            return null;
        } else {
            for (int i = 0; i < jobFairSceneModelPage.getRecords().size(); i++) {
                TrainTypeConfigSonModel trainTypeConfigSonModel = trainTypeConfigSonMapper.selectById(jobFairSceneModelPage.getRecords().get(i).getSid());
                ConfigModel configModel2 = configService.getModelById(trainTypeConfigSonModel.getConfigId());
                trainTypeConfigSonModel.setConfigIdText(configModel2.getName());
                SkillTrainingModel skillTrainingModel = skillTrainingMapper.selectById(trainTypeConfigSonModel.getTrainId());
                jobFairSceneModelPage.getRecords().get(i).setTrainTypeConfigSonModel(trainTypeConfigSonModel);
                jobFairSceneModelPage.getRecords().get(i).setSkillTrainingModel(skillTrainingModel);
            }
            return jobFairSceneModelPage;
        }
    }

    @Override
    public String applicationExamine(Integer id, Integer status, String remark) {
        TrainEnrollModel trainEnrollModel = trainEnrollMapper.selectById(id);
        if (trainEnrollModel != null) {
            trainEnrollModel.setStatus(status);
            if (remark != null && !remark.equals("")) {
                trainEnrollModel.setRemark(remark);
            }
            trainEnrollMapper.updateById(trainEnrollModel);
            return "操作成功";
        } else {
            return "该记录不存在";
        }
    }

}
